// pages/submit.js

const api = require('../../request/request.js')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    departmentName:"",
    departmentId:"",
    name:"",
    tel:"",
    phone:"",
    recordCertificateNumber:"",
    factory:"",//厂家
    companyPeopleCount:"",
    record:"1",//备案证明
    isGreenCode:"1",//是否绿码
    reasons:"",//来访事由
    beginTime:"",//开始时间
    recordTime:"",//预约时间
    endTime:"",//结束时间
    healthyStatus:"2",//身体状态
    temperature:"",//体温
    timeIndex:"0",
    timeShow:'1',//AM/PA  下标index
    loading:false,
    redioItems:[
      {value:'1',name:'是',checked:true},
      {value:'2',name:'否'}
    ],
    redioItems1:[
      {value:'1',name:'是'},
      {value:'2',name:'否',checked:true}
    ],
    timeState:[
      {value:'1',name:'AM',checked:true},
      {value:'2',name:'PM'}
    ],
    objectArray:['美国', '中国', '巴西', '日本']

  },
  
    /*
    下周四
    */
   lastFir(){
    var Stamp;
    Stamp = new Date(); 
    var num = 7-Stamp.getDay()+4;
    Stamp.setDate(Stamp.getDate() + num);
    
    var year = Stamp.getFullYear(); //获取完整的年份(4位,1970-????)
    var month = Stamp.getMonth() +1; //获取当前月份(0-11,0代表1月)
    var mvar ='';
    if(month<10){
    mvar = '0' + month;
    }else{
    mvar = month+'';
    }
    var day = Stamp.getDate();
    var dvar ='';
    if(day<10){
    dvar = '0' + day;
    }else{
    dvar = day+'';
    } 
    let recordTime = year+"-"+mvar+'-'+dvar
    console.log(recordTime)
    this.setData({
      recordTime:recordTime
    })
   },
  radioChange(e){
    const items = this.data.redioItems
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }
    this.setData({
      redioItems:items,
      isGreenCode:e.detail.value
    })  
  },
  radioChange1(e){
    const items = this.data.redioItems1
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }
    this.setData({
      redioItems1:items,
      healthyStatus:e.detail.value
    })  
  },
    clearForm(){
      this.setData({
        name:"",
        tel:"",
        factory:"",//厂家
        isGreenCode:"1",//是否绿码
        reasons:"",//来访事由
        phone:"",
        companyPeopleCount:"",
        recordCertificateNumber:"",
        // recordTime:"",//预约时间
        // beginTime:"",//开始时间
        // endTime:"",//结束时间
        // healthyStatus:"2",//身体状态
        temperature:"",//体温
        // timeIndex:"0",
        // timeShow:'1',//AM/PA  下标index
      })
    },
    setName(e){
      this.setData({name:e.detail.value})
    },
    setCount(e){
      this.setData({companyPeopleCount:e.detail.value})
    },
    setFactory(e){
      this.setData({factory:e.detail.value})
    },
    setReasons(e){
      this.setData({reasons:e.detail.value})
    },
    setRecordCertificateNumber(e){
      this.setData({recordCertificateNumber:e.detail.value})
    },
    setTel(e){
      this.setData({phone:e.detail.value})
    },
    setTemperature(e){
      this.setData({temperature:e.detail.value})
    },
      /*科室选择*/
  bindPickerChange(e){
    let departmentName = this.data.objectArray[e.detail.value]
    let departmentId = '';
    this.data.departmentList.forEach(el=>{
      if(el.departmentName==departmentName){
        departmentId = el.id
      }
    })
    console.log('picker发送选择改变，携带值为', departmentId)
    wx.removeStorageSync('departmentId')
    wx.removeStorageSync('departmentName')
    wx.setStorageSync('departmentId', departmentId)
    wx.setStorageSync('departmentName', departmentName)
    this.setData({
      departmentName:departmentName,
      departmentId:departmentId
    })
  },
      /*
    获取科室
    */
   getDepartment(){
    let url ="/department/getByMapList"
      api.AjaxGet(url,(res)=>{
        console.log(res)
        if(res.code=="10000"){
          let data = res.data
          let list = []
          data.forEach(v=>{
            console.log(v)
            list.push(v.departmentName)
          })
          // this.data.list = res.data.data
          this.setData({
            // departmentName:list[0],
            departmentList: res.data,
            objectArray:list,
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
        }
        })
   },
    submit(){
      let name,tel,factory,isGreenCode,reasons,healthyStatus,temperature,wechatUrl,wechatName,recordTime,departmentId,companyPeopleCount,phone,recordCertificateNumber;
      departmentId=this.data.departmentId;
      name=this.data.name;
      phone=this.data.phone;
      tel=this.data.tel;
      companyPeopleCount=this.data.companyPeopleCount
      factory=this.data.factory;
      isGreenCode=this.data.isGreenCode;
      recordTime=this.data.recordTime+" 00:00:00";
      healthyStatus=this.data.healthyStatus;
      temperature=this.data.temperature;
      recordCertificateNumber=this.data.recordCertificateNumber;
      reasons=this.data.reasons;
      wechatUrl = wx.getStorageSync('wechatUrl')
      wechatName = wx.getStorageSync('wechatName')
      if(!departmentId&&!name&&!tel&&!factory&&!isGreenCode&&!healthyStatus&&!temperature&&!reasons&&!companyPeopleCount&&!phone&&!recordCertificateNumber){
        wx.showToast({
          title: '请完善信息',
          icon: 'none',
          duration: 2000
        })
        return
      }
      let params={
        departmentId:departmentId,
        name:name,
        tel:tel,
        phone:phone,
        recordCertificateNumber:recordCertificateNumber,
        factory:factory,
        isGreenCode:isGreenCode,
        companyPeopleCount:companyPeopleCount,
        recordTime:recordTime,//预约时间
        healthyStatus:healthyStatus,
        temperature:temperature,
        reasons:reasons,
        recordStatus:"1",
        wechatUrl:wechatUrl,
        wechatName:wechatName
      }
      console.log(params)
      // return
      let url ="/talks_record/insert"
      api.AjaxPost(url,params,(res)=>{
        console.log(res)
        if(res.code=="10000"){
          // this.data.list = res.data.data
          const status = wx.getStorageSync('status')
          wx.navigateTo({
            url: '../lineList/lineList',
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
        }
        })
    },
//   onLaunch:function(){
// const api = require('../../request/request.js')

//   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      tel:wx.getStorageSync('tel')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getDepartment()
    let status = wx.getStorageSync('status')
    this.setData({
      status:status,
    })
    this.lastFir()//下周四
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


})